package com.leyou.common.pojo;

import com.fasterxml.jackson.core.type.TypeReference;
import com.leyou.common.utils.JsonUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Map;

/**
 * @author Eric
 * @version v1.0
 * @package com.leyou.common.pojo
 * @date 2019/6/21 15:05
 * @description:
 **/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    String name;
    Integer age;


    /**
     * 测试JsonUtils
     * @param args
     */
    public static void main(String[] args) {

        User user = new User();
        //toString
        String userJson = JsonUtils.toString(user);
        System.out.println("userJson = " + userJson);

        //toBean
        User user1 = JsonUtils.toBean(userJson, User.class);
        System.out.println("user1 = " + user1);

        //toList
        String listJson ="[12,-5,85,45,79]";
        List<Integer> integers = JsonUtils.toList(listJson, Integer.class);
        System.out.println("integers = " + integers);

        //toMap
        String mapJson = "{\"name\":\"Jack\", \"age\": \"21\"}";
        Map<String, String> map = JsonUtils.toMap(mapJson, String.class, String.class);
        System.out.println("map = " + map);


        //反序列化的结果比较复杂时，通过这个方法转换:nativeRead
        String json = "[{\"name\":\"Jack\", \"age\": \"21\"}, {\"name\":\"Rose\", \"age\": \"18\"}]";
        List<Map<String, String>> maps = JsonUtils.nativeRead(json, new TypeReference<List<Map<String, String>>>() {
        });
        for (Map<String, String> stringMap : maps) {

            System.out.println("stringMap = " + stringMap);
        }

    }
}
